package ds.stack.arraystack;

import java.util.LinkedHashMap;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 用数组实现栈
 * @date 2022/9/12 15:32
 */
public class ArraryStack {

    /**
     * 定义存储类型数组
     */
    private String[] items;

    /**
     *定义栈的大小
     */
    private int count;
    /**
     * 下标
     */
    private int index;

    private Object lock = new Object();

    /**
     申请一个长度为n的栈
     *
     */
    public ArraryStack(int n) {
        this.items = new String[n];
        this.count = n;
        this.index = 0;
    }


    /**
     * 入栈
     */
    public void push(String str){
        synchronized (lock){
            if(index<=count) {
                items[index] = str;
                index++;
            }else{
                System.out.println("队列已满");
            }
        }
    }
    /**
     * 出栈操作
     */
    public String pop(){
        String tmp = null ;
        if(count == 0){
            return null;
        }
        synchronized (lock){
            if(count>0){
                tmp = items[count-1];
                --count;
            }

        }
        return tmp;
    }


}


